<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\File;
use app\admin\controller\Base;
class Brand extends Base
{
	public function brand()
	{

		return view('brand/brand');
	}
	public function brand_list()
	{
		$keyword=input('get.keyword');
		$where="brand_name like '%$keyword%'";
	    //总条数
        $count=Db('brand')->where($where)->count();

        //每页显示条数
        $pageSize=3;
        //总页数
        $pagenum=ceil($count/$pageSize);
        //当前页
        $page=input('get.page',1);
        //上一页
        $up=$page-1<1?1:$page-1;
        $next=$page+1>$pagenum?$pagenum:$page+1;
        $end=$pagenum;
        //偏移量
        $limit=($page-1)*$pageSize;
        $data['res']=Db('brand')->where($where)->limit($limit,$pageSize)->select();
        $data['yema']=array('page'=>$page,'up'=>$up,'next'=>$next,'end'=>$end,'count'=>$count,'pagenum'=>$pagenum,'pageSize'=>$pageSize);
         if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
         	echo json_encode($data);
         }else{
         	return $this->fetch('brand',['data'=>$data]);
		/*	$this->assign('data',$data);
			return view('brand/brand');*/
         }
        
		
	}
	public function brand_add()
	{
		return view('brand/addbrand');
	}
	public function brand_add_do()
	{
		$data=input('post.');
		$file=request()->file('brand_logo');
		$info=$file->move(ROOT_PATH.'public'.DS.'uploads');
		// var_dump($info);
		$filename=$info->getSaveName();
		$data['brand_logo']=$filename;
		// var_dump($data);
		$res=Db('brand')->insert($data);
		if($res){
			$this->success('添加品牌成功','brand/brand_list');
		}else{
			$this->error('添加品牌失败','brand/brand_add');
		}
	}
	public function brand_del()
	{
		$brand_id=input('get.id');
		$res=Db('brand')->where('brand_id',$brand_id)->delete();
		if($res){
			$this->success('删除品牌成功','brand/brand_list');
		}else{
			$this->error('删除品牌失败','brand/brand_add');
		}
	}
	public function brand_status()
	{
		$newsdian=input('post.newsdian');
		$where=input('post.where');
		// var_dump($newsdian);
		$sql="update brand set is_show='$newsdian' where brand_id='$where'";
		$res=Db('brand')->execute($sql);
		if ($res) {
			echo json_encode(1);
		}
	}
}